package yt.kpool.activity.setting;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import org.litepal.crud.DataSupport;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yt.kpool.R;
import yt.kpool.activity.miner.MinerChartActivity;
import yt.kpool.bean.BaseBean;
import yt.kpool.bean.FastLookAccountBean;
import yt.kpool.bean.LineChartTestBean;
import yt.kpool.bean.MinerCharBean;
import yt.kpool.network.HttpUtils;
import yt.kpool.network.MyGson;
import yt.kpool.network.NetHelperNew;

import yt.kpool.util.NetWorkUtil;

import yt.kpool.util.TitleControl;
import yt.kpool.util.ToastUtil;
import yt.kpool.view.dialog.ErrorDialog;
import yt.kpool.view.dialog.LoadingDialog;
import yt.kpool.view.dialog.SuccessDialog;

/**
 * 添加账户
 */
public class AddAcountActivity extends AppCompatActivity {

    @BindView(R.id.edit_name)
    EditText edit_name;
    @BindView(R.id.tv_remark)
    EditText edit_remark;
    private LoadingDialog loadingDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_add_acount);
        ButterKnife.bind(this);
        initTitle();
    }
    private void initTitle() {
        TitleControl titleControl = new TitleControl(this);
        titleControl.setTitle("添加挖矿账户");

        loadingDialog=new LoadingDialog(this,0);
    }
    @OnClick({R.id.btn_submit})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_submit://确定
                if ("".equals(edit_name.getText().toString().trim()))
                {
                    ToastUtil.showToast(this,"请输入挖矿账户名称");
                    break;
                }else
                {
                    addChildAccount(edit_name.getText().toString().trim(),edit_remark.getText().toString().trim());

                }

                break;

        }
    }

    private void addChildAccount(String name,String remark) {
        if (NetWorkUtil.check(this)) {
            loadingDialog.show();
            NetHelperNew.AddChildAccount(name,remark, new HttpUtils.HttpCallback() {
                @Override
                public void onSuccess(String data) {
                    Log.i("-->", "添加子账户：" + data);
                    loadingDialog.dismiss();
                    BaseBean baseBean = MyGson.getInstance().fromJson(data, BaseBean.class);


                    if (baseBean.getType() == 1) {
                        new SuccessDialog(AddAcountActivity.this, baseBean.getContent(), new SuccessDialog.OnSuccessListener() {
                            @Override
                            public void getResult() {
                                setResult(20,getIntent());
                                finish();
                            }
                        }).show();
                    } else {

                        new ErrorDialog(AddAcountActivity.this, baseBean.getContent()).show();
                    }
                }
                @Override
                public void onError(String msg) {
                    super.onError(msg);

                    loadingDialog.dismiss();
                    ToastUtil.showToast(AddAcountActivity.this, msg);
                }
            });

        }
    }
}
